using System;
using System.Collections;

using NHibernate;

using Domain;

namespace Server.Manager
{
    class PlayerManager
    {
        public static Player getPlayer(string name)
        {
            try
            {
                // Open session
                ISession session = Database.Factory.OpenSession();

                // Retrieve object from database
                Player obj = (Player) session.Load(typeof(Player), name);

                // Close session
                session.Close();

                // Return object
                return obj;
            }

            catch (Exception)
            {
                return null;
            }
        }

        public static ArrayList getPlayers()
        {
            ArrayList collection = new ArrayList();

            try
            {
                // Open session
                ISession session = Database.Factory.OpenSession();

                // Retrieve object list from database
                collection = (ArrayList) session.CreateCriteria(typeof(Player)).List();

                // Close session
                session.Close();
            }

            catch (Exception)
            {
                // Error
            }

            return collection;
        }

        public static bool insertPlayer(Player obj)
        {
            try
            {
                // Open session and transaction
                ISession session = Database.Factory.OpenSession();
                ITransaction transaction = session.BeginTransaction();

                // Save object in session
                session.Save(obj);

                // Commit changes
                transaction.Commit();

                // Close session
                session.Close();

                return true;
            }

            catch (Exception)
            {
                return false;
            }
        }

        public static bool updatePlayer(Player obj)
        {
            try
            {
                // Open session and transaction
                ISession session = Database.Factory.OpenSession();
                ITransaction transaction = session.BeginTransaction();

                // Save object in session
                session.Update(obj);

                // Commit changes
                transaction.Commit();

                // Close session
                session.Close();

                return true;
            }

            catch (Exception)
            {
                return false;
            }
        }

        public static bool deletePlayer(Player obj)
        {
            try
            {
                // Open session and transaction
                ISession session = Database.Factory.OpenSession();
                ITransaction transaction = session.BeginTransaction();

                // Save object in session
                session.Delete(obj);

                // Commit changes
                transaction.Commit();

                // Close session
                session.Close();

                return true;
            }

            catch (Exception)
            {
                return false;
            }
        }
    }
}
